/*
 * @lc app=leetcode.cn id=203 lang=swift
 *
 * [203] 移除链表元素
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public var val: Int
 *     public var next: ListNode?
 *     public init() { self.val = 0; self.next = nil; }
 *     public init(_ val: Int) { self.val = val; self.next = nil; }
 *     public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
 * }
 */
class Solution {
    func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
        // 虚拟头结点
        let dymmyHead = ListNode(0)
        dymmyHead.next = head
        var tmp: ListNode? = dymmyHead
        while tmp?.next != nil {
            if tmp?.next?.val == val {
                tmp?.next = tmp?.next?.next
            } else {
                tmp = tmp?.next
            }
        }
        return dymmyHead.next
    }
}
// @lc code=end

